<?php
defined('_XR_EXEC') or die();
/**
 * 逻辑处理类 
 */
class report_send_Model extends report_base_model
{

	/* 乐园卡配送明细报表 */
	public function send1($formdata){
		$db = factory::getdbo();
		if(!isset($formdata['sendpi_id']))    $formdata['sendpi_id']  = '0';
		
		$rs_sendpi = $db->loadassoc('select * from t_sendpi where deleted=0 and id="'.$formdata['sendpi_id'].'" ');
		if(!$rs_sendpi){
			exit('没有找到配送记录');
		}
		$rsl = $db->loadassoclist('
			select 
			c.username,
			c.name,
			a.vege_num,
			d.code as vege_code,
			d.name as vege_name,
			e.type_name as vege_type_name ,
			f.logo_name as vege_logo_name,
			g.val as classes
			from t_send_vege a 
			left join t_send b on a.send_id=b.id 
			left join t_user c on b.user_id=c.id 
			left join t_vege d on a.vege_id=d.id 
			left join t_vegetype e on d.type_id=e.id 
			left join t_vegelogo f on d.logo_id=f.id 
			left join t_list_user_classes g on c.classes=g.id 
			left join t_sendpi h on a.sendpi_id=h.id
			where a.sendpi_id="'.$formdata['sendpi_id'].'" and a.vege_num>0 and h.deleted=0 order by a.send_id ');
		$s = '
		<table border="1">
			<thead>
				<tr>
					<th>序号</th>
					<th>配送日期</th>
					<th>配送批次编号</th>
					<th>会员类型</th>
					<th>会员编号</th>
					<th>姓名</th> 
					<th>品牌</th> 
					<th>种类</th> 
					<th>菜品</th> 
					<th>份数</th> 
				</tr>
			</thead>
			<tbody>
		';
		foreach($rsl as $k=>$v){
			$s .= '
				<tr>
					<td>'.($k+1).'</td>
					<td>'.$rs_sendpi['senddate'].'</td>
					<td>'.$rs_sendpi['code'].'</td>
					<td>'.$v['classes'].'</td>
					<td>'.$v['username'].'</td>
					<td>'.$v['name'].'</td>
					<td>'.$v['vege_logo_name'].'</td>
					<td>'.$v['vege_type_name'].'</td>
					<td>'.$v['vege_name'].'('.$v['vege_code'].')</td>
					<td>'.$v['vege_num'].'</td>
				</tr>
			';
		} 
		$s .= '
			</tbody>
		</table>
		';
		return $s; 

	}
	
	

	/* 乐园卡配送菜品汇总 */
	public function send2($formdata){
		$db = factory::getdbo();
		
 	  #查询条件
		$sql_where = ' where b.deleted=0 and b.status in ('.$formdata['status'].') ';
		if($formdata['b_date'] != ''){
			$sql_where .= ' and b.senddate>="'.$formdata['b_date'].'" ';
		}
		if($formdata['e_date']!=''){
			$sql_where .= ' and b.senddate<="'.$formdata['e_date'].'"';	
		}
		$sql = "
		select 
		d.type_name as vegetype_name,
		e.logo_name as vegelogo_name,
		c.name as vege_name,
		a.vege_id,
		sum(vege_num) as vege_num
		from `t_send_vege` a 
		left join t_sendpi b on a.sendpi_id=b.id 
		left join t_vege c on a.vege_id=c.id 
		left join t_vegetype d on c.type_id=d.id 
		left join t_vegelogo e on c.logo_id=e.id 
		".$sql_where."
		group by a.vege_id
		" ;
		$rsl= $db->loadassoclist($sql);		
	//	debug::show($rs1,$sql);
		if(!$rsl){
			exit('没有找到记录');
		}
		$s = '
		<table border="1">
			<thead>
				<tr>
					<th>序号</th>
					<th>菜品品牌</th>
					<th>菜品类别</th>
					<th>菜品名称</th>
					<th>总份数</th>
				</tr>
			</thead>
			<tbody>
		';
		foreach($rsl as $k=>$v){
			$s .= '
				<tr>
					<td>'.($k+1).'</td>
					<td>'.$v['vegelogo_name'].'</td>
					<td>'.$v['vegetype_name'].'</td>
					<td>'.$v['vege_name'].'</td>
					<td>'.$v['vege_num'].'</td>
				</tr>
			';
		} 
		$s .= '
			</tbody>
		</table>
		';
		return $s; 

	}	

	/* 乐园卡配送批次出货单 */
	public function send3($formdata){
		$db = factory::getdbo();
		if(!isset($formdata['sendpi_id']))    $formdata['sendpi_id']  = '0';
		$rs_sendpi = $db->loadassoc('select * from t_sendpi where deleted=0 and id="'.$formdata['sendpi_id'].'" ');
		if(!$rs_sendpi){
			exit('没有找到配送记录');
		}
		$rs_send = $db->loadassoclist('select a.* from t_send a left join t_user b on a.user_id=b.id left join t_list_user_classes c on b.classes=c.id where a.sendpi_id="'.$rs_sendpi['id'].'" order by c.show_index,a.user_id  ');
		
		
		foreach($rs_send as &$vv){
			$vv['rs_send_vege'] = $db->loadassoclist('
				select 
					c.username,
					c.name,
					a.vege_num,
					d.code as vege_code,
					d.name as vege_name,
					e.type_name as vege_type_name ,
					f.logo_name as vege_logo_name 
				from t_send_vege a 
					left join t_send b on a.send_id=b.id 
					left join t_user c on b.user_id=c.id 
					left join t_vege d on a.vege_id=d.id 
					left join t_vegetype e on d.type_id=e.id 
					left join t_vegelogo f on d.logo_id=f.id 
				where a.send_id="'.$vv['id'].'" and a.vege_num>0  order by e.show_index 
			');
			$vv['userinfo'] = $db->loadassoc('
				select
					a.username,
					a.name,
					a.province,
					a.city,
					a.area,
					a.sendaddr,
					a.sendman,
					a.sendphone,
					a.sendtime,
					a.bsendman,
					a.bsendphone,
					a.remark,
					a.sendremark,
					b.val as classes,
					c.val as zone
				from t_user a 
				left join t_list_user_classes b on a.classes=b.id 
				left join t_list_zone c on a.zone=c.id 
				where a.id="'.$vv['user_id'].'" 
			');
		}
		// debug::show($rs_send);
		$s = '';
		
		$min_row = 12;
		$r_cungen =  array(
			'<td colspan="1" style="width:150px;background-color:#FDE9D9"> 发货员： </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">   </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9"> 配送员： </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">   </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9"> </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9"> </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9;font-size:16px">会员签收：</td>',
			'<td colspan="1" style="width:150px;border:0;background-color:#FDE9D9"> </td>'
		);
		$s_cungen_over = '<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>';
		
		
		foreach($rs_send as $k=>$v){
			# 当前批次，当前分支，当前会员是否存在自选信息
			$rs_send[$k]['isbyself'] = factory::module('send')->isbyself($v['id'])===true?"1":"0";
		}	
		$rs_send_real = array();
		foreach($rs_send as $k=>$v){
			if($v['isbyself'] != '1'){
				continue;
			}
			$rs_send_real[] = $v;
		}	
		foreach($rs_send as $k=>$v){
			if($v['isbyself'] != '0'){
				continue;
			}
			$rs_send_real[] = $v;
		}	
		
		foreach($rs_send_real as $k=>$v){
			$senddate = $db->loadresult('select senddate from t_sendpi where id="'.$v['sendpi_id'].'" ');
			$b_senddate = date('Y-m-d',dates::getWeekFirst(strtotime($senddate)));
			$rs_cishu = $db->loadresult('select count(a.id) from t_send a left join t_sendpi b on a.sendpi_id=b.id where a.user_id="'.$v['user_id'].'" and b.senddate<"'.$senddate.'" and b.senddate>="'.$b_senddate.'" ');
	    $s .= '
			<table border="1">
				<tr>
					<td colspan="4" style="height:50px;text-align:center;font-size:16px;font-weight:bold;border-right:0">田园会|会员出货单</td>
					<td colspan="1" style="text-align:right;font-size:12px;border-left:0">
						状态：'.($v['isbyself']==='1'?"自选":"默认").' <br />
						配送次数：第 '.(intval($rs_cishu)+1).' 次/周
					</td>
					<td colspan="1" style="text-align:center;font-size:16px;font-weight:bold;background-color:#FDE9D9;width:150px;">公司存根单</td>
				</tr>
				<tr>
					<td colspan="2" style="width:120px;">配送批次编号：</td>
					<td colspan="1" style="text-align:center">'.$rs_sendpi['code'].'</td>
					<td colspan="1">会员类型：</td>
					<td colspan="1" style="text-align:center">'.$v['userinfo']['classes'].'</td>
					<td colspan="1" style="background-color:#FDE9D9">配送批次编号：</td>
				</tr>
				<tr>
					<td colspan="2" style="width:120px;">会员姓名：</td>
					<td colspan="1" style="width:150px;text-align:center;font-size:16px">'.$v['userinfo']['name'].'</td>
					<td colspan="1" style="width:150px;">会员编号：</td>
					<td colspan="1" style="width:150px;text-align:center">'.$v['userinfo']['username'].'</td>
					<td colspan="1" style="width:150px;background-color:#FDE9D9">'.$rs_sendpi['code'].'</td>
				</tr>
				<tr>
					<td colspan="2">配送日期：</td>
					<td colspan="1"  style="text-align:center">'.$rs_sendpi['senddate'].'</td>
					<td colspan="1">配送时段：</td>
					<td colspan="1"  style="text-align:center">'.$v['userinfo']['sendtime'].'</td>
					<td colspan="1" style="background-color:#FDE9D9">质检员：</td>
				</tr>
				<tr>
					<td colspan="1" style="text-align:center;background-color:#BFBFBF;width:50px;">序号</td>
					<td colspan="1" style="text-align:center;background-color:#BFBFBF;width:100px;">品牌</td>
					<td colspan="1" style="text-align:center;background-color:#BFBFBF;width:150px;">品类</td>
					<td colspan="1" style="text-align:center;background-color:#BFBFBF;width:150px;">菜品</td>
					<td colspan="1" style="text-align:center;background-color:#BFBFBF;width:150px;">份数</td>
					<td colspan="1" style="background-color:#FDE9D9;width:150px;"> </td>
				</tr>			
			';
			$fenshu_sum = 0;
			$now_sendvege_id = 1;
			foreach($v['rs_send_vege'] as $key=>$val){
				$fenshu_sum += intval($val['vege_num']);
				$s .= '
				<tr>
					<td colspan="1" style="width:50px;text-align:center">'.$now_sendvege_id.'</td>
					<td colspan="1" style="width:100px;text-align:center">'.$val['vege_logo_name'].'</td>
					<td colspan="1" style="width:150px;text-align:center">'.$val['vege_type_name'].'</td>
					<td colspan="1" style="width:150px;text-align:center;font-size:16px">'.$val['vege_name'].'</td>
					<td colspan="1" style="width:150px;text-align:center;font-size:16px">'.$val['vege_num'].'</td>
				';
				if( $now_sendvege_id > count($r_cungen) ){
					$s .= $s_cungen_over;
				}else{
					$s .= $r_cungen[$key];
				}
				$s .='
				</tr>				
				';
				$now_sendvege_id++;
			}
			if($now_sendvege_id < $min_row){
				$shengyu_fornum = $min_row - $now_sendvege_id;
				for($ii=0; $ii<$shengyu_fornum; $ii++){
					$s .= '
					<tr>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1"></td>
						<td colspan="1"></td>
						<td colspan="1"></td>';
					if( $now_sendvege_id > count($r_cungen) ){
						$s .= $s_cungen_over;
					}else{
						$s .= $r_cungen[($now_sendvege_id - 1)];
					}
					$s .= '
					</tr>		
					';
					$now_sendvege_id++;
				}
			}
			$s .= '
				<tr>
					<td colspan="4" style="text-align:right">菜品合计：</td>
					<td colspan="1" style="text-align:center">'.$fenshu_sum.'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="2">所属区域：</td>
					<td colspan="1" style="text-align:center;width:150px">'.$v['userinfo']['province'].' '.$v['userinfo']['city'].' '.$v['userinfo']['area'].'</td>
					<td colspan="1">小区名称：</td>
					<td colspan="1" style="text-align:center">'.$v['userinfo']['zone'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="2">收货地址：</td>
					<td colspan="3" style="font-size:16px">'.$v['userinfo']['sendaddr'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="2">收货联系人：</td>
					<td colspan="1" style="text-align:center;font-size:16px">'.$v['userinfo']['sendman'].'</td>
					<td colspan="1">联系电话：</td>
					<td colspan="1" style="text-align:center;font-size:16px">'.$v['userinfo']['sendphone'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="2">紧急联系人：</td>
					<td colspan="1" style="text-align:center">'.$v['userinfo']['bsendman'].'</td>
					<td colspan="1">联系电话：</td>
					<td colspan="1" style="text-align:center">'.$v['userinfo']['bsendphone'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="2" style="text-align:center;font-size:14px;font-weight:bold;">配送特殊需求</td>
					<td colspan="3" style="">'.$v['userinfo']['sendremark'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="2" style="text-align:center;font-size:14px;font-weight:bold;">签收须知</td>
					<td colspan="3" style="height:100px;">请您当场与田园牧歌送货人员确认实际送货商品与出货单商品明细是否一致，检查商品有无质量问题，确认完毕后在送货单右下方签字确认。您或您委托收货人的签字表示您对商品数量及质量无异议。商品签收后请妥善保存，如因保存不善发生质量问题，田园牧歌有权不接受以此为由的退换货。</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
			</table>			
			<table border="0">
				<tr><td style="border:0">&nbsp;</td></tr>
			</table>
			';
		}
		return $s;
	}
	

	/* 乐园卡配送明细报表 */
	public function send4($formdata){
		$db = factory::getdbo();
		
		
		
		$sql_where = ' ';
		if($formdata['b_date'] != ''){
			$sql_where .= ' and h.senddate>="'.$formdata['b_date'].'" ';
		}
		if($formdata['e_date']!=''){
			$sql_where .= ' and h.senddate<="'.$formdata['e_date'].'"';	
		}		
/*		
		$rsl = $db->loadassoclist('
			select 
			
			
			c.username,
			c.name,
			a.vege_num,
			
			
			d.code as vege_code,
			d.name as vege_name,
			e.type_name as vege_type_name ,
			f.logo_name as vege_logo_name,
			
			
			g.val as classes,
			
			
		h.code as sendpi_code,
h.senddate as sendpi_senddate,
k.name as sendfen_name,
a.vege_id as vege_id,
a.vege_num as vege_num,
a.fixed as fixed,
a.sendpi_id,
a.sendfen_id,
a.send_id
			
			from t_send_vege a 
			left join t_send b on a.send_id=b.id 
			left join t_user c on b.user_id=c.id 
			left join t_vege d on a.vege_id=d.id 
			left join t_vegetype e on d.type_id=e.id 
			left join t_vegelogo f on d.logo_id=f.id 
			left join t_list_user_classes g on c.classes=g.id 
			left join t_sendpi h on a.sendpi_id=h.id
			left join t_sendfen k on a.sendfen_id=k.id
			where a.vege_num>0 and h.deleted=0  '.$sql_where.'  order by a.send_id ');*/
			
			$rsl = $db->loadassoclist('
			select  
		
b.user_id, 
h.code as sendpi_code,
h.senddate as sendpi_senddate,
k.name as sendfen_name,
a.vege_num, 
a.vege_id as vege_id,
a.vege_num as vege_num,
a.fixed as fixed,
a.sendpi_id,
a.sendfen_id,
a.send_id
			
			from t_send_vege a 
			left join t_send b on a.send_id=b.id 
			left join t_sendpi h on a.sendpi_id=h.id
			left join t_sendfen k on a.sendfen_id=k.id
			where a.vege_num>0 and h.deleted=0  '.$sql_where.'  order by a.send_id ');		
		foreach($rsl as $kk=>$vv){
			# 当前批次，当前分支，当前会员是否存在自选信息
			$isbyself = factory::module('send')->isbyself($vv['send_id']);
			$rsl[$kk]['byself'] = $isbyself===true?"1":"0";
		}
		
		 
		$rs_veges = $db->loadassoclist('
			select 
				a.id,
				a.code,
				a.name,
				b.type_name,
				c.logo_name 
			from t_vege a 
			left join t_vegetype b on a.type_id=b.id
			left join t_vegelogo c on a.logo_id=c.id 
		','id');
		$rs_users = $db->loadassoclist('
			select 
				a.id,
				a.name,
				a.username,
				b.val as classes
			from t_user a 
			left join t_list_user_classes b on a.classes = b.id
		','id');
		
		
		$s = '
		<table border="1">
			<thead>
				<tr>
					<th>序号</th>
					<th>配送日期</th>
					<th>配送批次编号</th>
					<th>配送分支标识</th>
					<th>会员类型</th>
					<th>会员编号</th>
					<th>姓名</th> 
					<th>品牌</th> 
					<th>种类</th> 
					<th>菜品</th> 
					<th>份数</th> 
					<th>是否可选菜品</th> 
					<th>是否默认选择</th> 
				</tr>
			</thead>
			<tbody>
		';
	 
		foreach($rsl as $k=>$v){
			$s .= '
				<tr>
					<td>'.($k+1).'</td>
					<td>'.$v['sendpi_senddate'].'</td>
					<td>'.$v['sendpi_code'].'</td>
					<td>'.$v['sendfen_name'].'</td>
					<td>'.$rs_users[$v['user_id']]['classes'].'</td>
					<td>'.$rs_users[$v['user_id']]['username'].'</td>
					<td>'.$rs_users[$v['user_id']]['name'].'</td>
					<td>'.$rs_veges[$v['vege_id']]['logo_name'].'</td>
					<td>'.$rs_veges[$v['vege_id']]['type_name'].'</td>
					<td>'.$rs_veges[$v['vege_id']]['name'].'('.$rs_veges[$v['vege_id']]['code'].')</td>
					<td>'.$v['vege_num'].'</td>';
			if($v['fixed'] == '0'){
				$s .='<td>是</td>';
			}else{
				$s .='<td>否</td>';
			}
			if($v['byself'] == '0'){
				$s .='<td>是</td>';
			}else{
				$s .='<td>否</td>';
			}
			$s .='	
				</tr>
			';
		} 
		$s .= '
			</tbody>
		</table>
		';
		return $s; 

	}	
	
	
	
}

?>